
#include "com_toast.h"
#include "page_base.h"
#include "page_manager.h"
#include "img_common.h"
#include "pt_ipc.h"
#include "pt_info.h"
#include "pt_dev.h"
#include "sys_font.h"
#include "pt_fs.h"

static com_toast_t  uct = {
    .bg_txt    = NULL,
    .txt       = NULL,
    .die_timer = NULL,
};


static void com_toast_die_timer_callback(struct _lv_timer_t *t)
{
    ui_com_toast_destroy();
}

int ui_com_toast_create(int timeout,const char* string)
{
    lv_coord_t x  = 0;
    lv_coord_t y  = 0;
    lv_coord_t w  = 0;
    lv_coord_t h  = 0;
    lv_coord_t pw = 0;
    lv_coord_t ph = 0;

    pw = lv_disp_get_hor_res(NULL);
    ph = lv_disp_get_ver_res(NULL);
    //****************************************************************
    if(!uct.bg_txt){
        uct.bg_txt = lv_obj_create(lv_layer_top());
    }

    w = 200;
    h = 32;
    x = (pw - w)/2;
    y = ph - h - 20;

    lv_obj_set_pos(uct.bg_txt,x,y);
    lv_obj_set_size(uct.bg_txt,w,h);

	lv_obj_set_style_radius(uct.bg_txt, 30, LV_PART_MAIN);
	lv_obj_set_style_border_width(uct.bg_txt, 0, LV_PART_MAIN);
	lv_obj_set_style_border_color(uct.bg_txt, lv_color_make(0x00, 0x00, 0x00), LV_PART_MAIN);
	lv_obj_set_style_border_opa(uct.bg_txt, LV_OPA_0, LV_PART_MAIN);

	lv_obj_set_style_outline_width(uct.bg_txt, 0, LV_PART_MAIN);
	lv_obj_set_style_shadow_width(uct.bg_txt, 0, LV_PART_MAIN);
	lv_obj_set_scrollbar_mode(uct.bg_txt, LV_SCROLLBAR_MODE_OFF);
	lv_obj_set_style_pad_all(uct.bg_txt, 0,0);

    #if 0
    lv_obj_set_style_bg_opa(uct.bg_txt,LV_OPA_0,0);
    #else
    lv_obj_set_style_bg_opa(uct.bg_txt,LV_OPA_80,0);
    lv_obj_set_style_bg_color(uct.bg_txt,lv_color_make(0xff,0xff,0xff),0);
    #endif
    //**********************************************************************************************

    if(!uct.txt){
        uct.txt = lv_label_create(uct.bg_txt);
    }
    lv_obj_set_pos(uct.txt,0,4);
    lv_obj_set_size(uct.txt,200,26);
    lv_obj_set_style_text_align(uct.txt,LV_TEXT_ALIGN_CENTER,0);
    lv_obj_set_style_text_opa(uct.txt,LV_OPA_100,LV_PART_MAIN);
    lv_obj_set_style_text_color(uct.txt,lv_color_make(0x33,0x33,0x33),LV_PART_MAIN);
    lv_obj_set_style_text_font(uct.txt, lv_font_get(20), LV_STATE_DEFAULT);

    lv_label_set_text(uct.txt,string);
    //**********************************************************************************************
    if(!timeout){
        timeout = 2000;
    }
    if(!uct.die_timer){
        uct.die_timer = lv_timer_create(com_toast_die_timer_callback,timeout,NULL);
    }else{
        lv_timer_reset(uct.die_timer);
    }
    //**********************************************************************************************

    return 0;

}

int ui_com_toast_destroy(void)
{
    if(uct.die_timer){
        lv_timer_del(uct.die_timer);
        uct.die_timer = NULL;
    }

    if(uct.bg_txt){
        lv_obj_del(uct.bg_txt);
        uct.bg_txt = NULL;
        uct.txt    = NULL;
    }

    return 0;
}
